package ro.pub.cs.stadium.platform;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class InputDialog implements SelectionListener{
	
	private final Shell mainWnd;
	
	private final Shell inputDialog;
	private final Button okButton;
	private final Button cancelButton;
	
	private final Text textField;
	private final Label label;
	
	private int modalResult = -1;
	
	private String question = "";
	private String answer = "";
	
	public InputDialog(Shell mainWnd) {
		this.mainWnd = mainWnd;
		
		inputDialog = new Shell(mainWnd, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		inputDialog.setText("Input Requested");
		
		okButton = new Button(inputDialog, SWT.PUSH);
		okButton.setText("&OK");
		inputDialog.setDefaultButton(okButton);
		
		cancelButton = new Button(inputDialog, SWT.PUSH);
		cancelButton.setText("&Cancel");
		
		textField = new Text(inputDialog, SWT.SINGLE);
		textField.setText("");
		
		label = new Label(inputDialog, SWT.NONE);
		label.setText("Input:");
		
		FormLayout layout = new FormLayout();
		layout.marginWidth = layout.marginHeight = 10;
		inputDialog.setLayout(layout);
		
		FormData okData = new FormData();
		okData.bottom = new FormAttachment(100);
		okData.width = 100;
		okData.right = new FormAttachment(cancelButton, -10);
		okButton.setLayoutData(okData);
		
		FormData cancelData = new FormData();
		cancelData.right = new FormAttachment(100);
		cancelData.bottom = new FormAttachment(100);
		cancelData.width = 100;
		cancelButton.setLayoutData(cancelData);
		
		FormData textData = new FormData();
		textData.left = new FormAttachment(0);
		textData.right = new FormAttachment(100);
		textData.bottom = new FormAttachment(okButton, -10);
		textData.width = 300;
		textField.setLayoutData(textData);
		
		FormData labelData = new FormData();
		labelData.left = new FormAttachment(0);
		labelData.right = new FormAttachment(100);
		labelData.bottom = new FormAttachment(textField, -10);
		label.setLayoutData(labelData);
		
		inputDialog.pack();
		
		okButton.addSelectionListener(this);
		cancelButton.addSelectionListener(this);
	}
	
	public String getQuestion() {
		return question;
	}
	
	public void setQuestion(String question) {
		this.question = question; 
	}
	
	public String getAnswer() {
		return answer;
	}
	
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
	public int showModal() {
		modalResult = -1;
		
		label.setText(question);
		
		inputDialog.open();
		
		while (!inputDialog.isDisposed ()) {
			if (!inputDialog.getDisplay().readAndDispatch ())
				inputDialog.getDisplay().sleep ();
		}
		
		return modalResult;
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void widgetSelected(SelectionEvent e) {
		if (e.widget == okButton) {
			modalResult = 0;
			
			answer = textField.getText();
			
			inputDialog.close();
		}
		
		if (e.widget == cancelButton) {
			modalResult = -1;
			inputDialog.close();
		}
	}
}
